package com.atguigu.jxc.service.impl;


import com.atguigu.jxc.dao.DamageDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.DamageService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.UserService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: DamageServiceImpl
 * Package: com.atguigu.jxc.service.impl
 *
 * @Description:
 * @Author: xvyy
 * @Create: 2025/2/21 - 21:55
 * @Version: v1.0
 */
@Service
public class DamageServiceImpl implements DamageService {

    @Autowired
    private DamageDao damageDao;
    @Autowired
    private LogService logService;
    @Autowired
    private UserService userService;


    @Override
    public ServiceVO save(DamageList damageList, String damageListGoodsStr, HttpSession session) {
        if (damageListGoodsStr == null || damageListGoodsStr.isEmpty()) {
            return new ServiceVO<>(ErrorCode.NULL_POINTER_CODE, ErrorCode.NULL_POINTER_MESS);
        }
        if (damageList == null) {
            return new ServiceVO<>(ErrorCode.NULL_POINTER_CODE, ErrorCode.NULL_POINTER_MESS);
        }
        // 查询所有报损单数量
        Integer damageCount = damageDao.countDamageList() + 1;
        Gson gson = new Gson();
        // 从session中获取当前登录用户的ID
        User user = (User) session.getAttribute("currentUser");
        damageList.setUserId(user.getUserId());
        damageDao.saveDamageList(damageList);
        List<DamageListGoods> damageListGoodsList = gson.fromJson(damageListGoodsStr, new TypeToken<List<DamageListGoods>>() {
        }.getType());
        damageListGoodsList.forEach(damageListGoods -> {
            damageListGoods.setDamageListId(damageCount);
            damageDao.saveDamageListGoods(damageListGoods);
        });
        logService.save(new Log(Log.INSERT_ACTION, "新增商品报损单"));
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map<String, Object> list(String sTime, String eTime) {
        if (sTime == null || eTime == null) {
            // 查询全部报损单
            List<DamageList> damageLists = damageDao.list(sTime, eTime);
            Map<String, Object> map = new HashMap<>();
            map.put("rows", damageLists);
            return map;
        }
        // 查询所有报损单 范围是startTime到endTime
        List<DamageList> damageLists = damageDao.list(sTime, eTime);
        Map<String, Object> map = new HashMap<>();
        map.put("rows", damageLists);
        return map;
    }

    @Override
    public Map<String, Object> goodsList(Integer damageListId) {

        if (damageListId == null) {
            return Collections.emptyMap();
        }
        List<DamageListGoods> damageListGoods = damageDao.goodsList(damageListId);
        Map<String, Object> map = new HashMap<>();
        map.put("rows", damageListGoods);
        return map;
    }

}
